﻿/*jslint browser: true, cap: false, passfail: false, undef: false, white: false */
/*global SONGHAY YAHOO */

(function(){

    //Module-level members:
    var client = SONGHAY.ui.Client;

    SONGHAY.ui.GenericWebEditor =
    {
        buildDisplay: function()
        {
            var vContextName = (arguments.length > 0) ? arguments[0] : '';

            //TODO: Build any server exceptions:
            //if(self.displayBuilder.forServerMessage()) { return; }

            //The functional/querying/composeable builders sorted by name:
            self.displayBuilder.forCalendar();
            self.displayBuilder.forSearch();
            self.displayBuilder.forTextArea();
            self.displayBuilder.forWebForm();
            self.displayBuilder.forWebFormDocument();
            self.displayBuilder.forWebFormFragment();
            self.displayBuilder.forWebFormSegment();
            self.displayBuilder.forWebIndex();

            //Builders composed for named contexts:
            if (vContextName == 'initialize')
            {
                self.displayBuilder.forCommandsOnLoad();
                self.displayBuilder.forFormsOnLoad();
            }
        },

        calendar: null,
        calendarPickSource: null,
        calendarPickTarget: null,

        calendarSetDate: function()
        {
            //TODO: make null checks for calendarPickSource and calendarPickTarget

            var vAppendTime = true; //TODO: this should be set from some configuration location.

            var oDate = new Date();
            var vHours = oDate.getHours();
            vHours = (vHours < 10) ? '0' + vHours.toString() : vHours.toString();

            var oWidgetDate;

            if (vAppendTime)
            {
                var vMinutes = oDate.getMinutes();
                vMinutes = (vMinutes < 10) ? '0' + vMinutes.toString() : vMinutes.toString();

                var vSeconds = oDate.getSeconds();
                vSeconds = (vSeconds < 10) ? '0' + vSeconds.toString() : vSeconds.toString();

                //this === YAHOO.rasx.widgets.sampleCalendar:
                oWidgetDate = this.getSelectedDates()[0];

                this.oDomContainer.style.display = 'none';

                self.calendarPickTarget.value =
                    (oWidgetDate.getMonth() + 1) +
                    '/' +
                    oWidgetDate.getDate() +
                    '/' +
                    oWidgetDate.getFullYear() +
                    ' ' +
                    vHours +
                    ':' +
                    vMinutes +
                    ':' +
                    vSeconds;
            }
            else
            {
                //this === self.calendar:
                oWidgetDate = this.getSelectedDates()[0];

                this.oDomContainer.style.display = 'none';

                self.calendarPickTarget.value =
                    (oWidgetDate.getMonth() + 1) +
                    '/' +
                    oWidgetDate.getDate() +
                    '/' +
                    oWidgetDate.getFullYear();
            }
        },

        mouseOutLink: function(e)
        {
            var oSrcElement = YAHOO.util.Event.getTarget(e);
            var o = oSrcElement.style;

            if (oSrcElement.tagName.toLowerCase() == 'a')
            {
                if (o)
                {
                    o.color = SONGHAY.ui.Client.color;
                    o.backgroundColor = 'transparent';
                }
            }
        },

        searchTextBlur: function(e)
        {
            var o = YAHOO.util.Event.getTarget(e);
            if (o && (o.value !== undefined) && (o.value.length === 0)) { o.value = 'Search...'; }
        },

        searchTextFocus: function(e)
        {
            var o = YAHOO.util.Event.getTarget(e);
            if (o && o.value && (o.value == 'Search...')) { o.value = ''; }
        }
    };

    //Module-level "self":
    var self = SONGHAY.ui.GenericWebEditor;

})();
